
/* eslint-disable @typescript-eslint/no-explicit-any */
export const Copy = <T>(source: T): T => {
  return JSON.parse(JSON.stringify(source))
}

// 合并对象  -> 只合并 target 已有的并且基本类型相同的属性
export const Assign = (
  source: Record<string, any>,
  target: Record<string, any>,
): Record<string, any> => {
  if (
    Object.prototype.toString.call(source) !== '[object Object]' ||
    Object.prototype.toString.call(target) !== '[object Object]'
  ) {
    return target
  }

  for (const i in target) {
    if (
      Object.prototype.toString.call(source[i]) !==
      Object.prototype.toString.call(target[i])
    ) {
      continue
    }

    switch (Object.prototype.toString.call(target[i])) {
      case '[object Array]':
        if (JSON.stringify(target[i]) !== JSON.stringify(source[i]))
          target[i] = Copy(source[i])
        break
      case '[object Object]':
        Assign(source[i], target[i])
        break
      case '[object Boolean]':
      case '[object Number]':
      case '[object String]':
        target[i] = source[i]
        break
    }
  }
  return target
}
